package com.mogu.comment.controller;

import com.mogu.comment.pojo.CommentList;
import com.mogu.comment.pojo.Email;
import com.mogu.comment.service.CommentService;
import com.mogu.comment.service.EmailService;
import com.mogu.comment.service.FindUserService;
import com.mogu.entity.TComment;
import com.mogu.message.BaseMessageConf;
import com.mogu.message.MessageConf;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.time.ZonedDateTime;
import java.util.*;

/**
 * Description:不在能知，而在能行
 *
 * @author: 梁西华
 * @date: 2020/6/15/10:22
 **/
@RestController
@RequestMapping("/comment")
@Api(value = "评论相关接口", tags = {"评论相关接口"})
public class CommentController {

    @Autowired
    CommentService commentService;

    @Autowired
    EmailService emailService;

    @Autowired
    FindUserService findUserService;

    /**
     * 发表评论
     * @param comment
     * @return
     */
    @ApiOperation(value = "增加评论", notes = "增加评论")
    @PostMapping("/addComment")
    public String addComment(@RequestBody TComment comment){


        System.out.println(this.commentService.cacheInvalidation(comment));
        //通过用户id和评论内容判断是否为垃圾评论
        if (this.commentService.cacheInvalidation(comment)){
            //String COMMENT_IS_SPAM = "请输入有意义的评论内容！";
            System.out.println("评论无意义");
            return MessageConf.COMMENT_IS_SPAM;
        }else {
            //添加评论
            System.out.println("添加评论");
            UUID uuid = UUID.randomUUID();
            System.out.println(uuid.toString().replace("-",""));

            comment.setUid(uuid.toString().replace("-",""));


           /* ZonedDateTime now = ZonedDateTime.now();
            System.out.println(now);
            //将ZonedDateTime转为Date类型
            Date from = Date.from(now.toInstant());
            comment.setCreateTime(from);
            System.out.println(from);*/

            Date date = new Date();
            Timestamp timestamp = new Timestamp(date.getTime());
            comment.setCreateTime(timestamp);

            System.out.println(comment.toString());
            //添加评论
            this.commentService.addComment(comment);

            //通知被评论用户有新的评论
            Email email = new Email();

            email.setMessage("您有一条新的评论："+ comment.getContent());

            //根据被回复用户id查出用户邮箱
            email.setSendTo(this.findUserService.findEmailByBlogUid(comment.getToUserUid()));

            email.setSubject("您有一条新的评论");
            System.out.println(email.toString());

            String  s  = this.emailService.sendEmail(email);

            String success = "success";
            //String OPERATION_SUCCESS = "操作成功";
            return BaseMessageConf.OPERATION_SUCCESS;
            /*if (success.equals(s)){
                System.out.println("发送邮件成功");
                //String OPERATION_SUCCESS = "操作成功";
                return BaseMessageConf.OPERATION_SUCCESS;
            }else {
                System.out.println("发送邮件失败");
                return "邮件提醒发送失败";
            }*/
        }
    }

    /**
     * 根据博客id查询当前博客的所有评论
     * @param blogUid
     * @return
     */
    @ApiOperation(value = "获取评论列表", notes = "获取评论列表")
    @GetMapping("/findComment/{blogUid}")
    public List<CommentList> findComment(@PathVariable String blogUid){
        return this.commentService.findComment(blogUid);
    }

  /*  @Test
    public void dataTest(){
  *//*      SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = simpleDateFormat.format(new Date());*//*
        Date date = new Date();
        Timestamp timestamp = new Timestamp(date.getTime());
        System.out.println(timestamp);
    }*/
}
